#coding=utf-8
import urllib,json,urllib,os,shutil,time
#新建文件夹
def make_dir(path):
    if  os.path.exists(path):
        shutil.rmtree(path)
    os.mkdir(path)

#写图片文件
def writeImg(img_list,id,path,channel_id):
    index=0
    for img in img_list :
     if img.has_key("image"):
        index+=1
        imgUrl=img["image"]["remote_path"]
        #print imgUrl
        img=urllib.urlopen(imgUrl).read()
        img_path=path+str(channel_id)+"_"+str(id)+"_"+str(index)+".jpg"
        f = file(img_path,"wb")
        f.write(img)
    f.close()

#打印文本内容
def print_para(para_list):
    for para in para_list :
     if para.has_key("text"):
        print para["text"]

#获取详细
def get_detail(response,path,param,noteAddr):
   json_data = json.loads(response)["data"]
   channel_id=json_data["channel_info"]["id"]
   channel_name=json_data["channel_info"]["name"]
   comment_num=json_data["comment_num"]
   like_num=json_data["like_num"]
   visit_num=json_data["visit_num"]
   username=json_data["base_user"]["name"]#用户名
   if visit_num>param[0] and like_num>param[1] and comment_num>param[2]:
       if channel_id in [18,21,22,28,29]:
         para_list=json_data["paragraph_list"]
         id=json_data["id"]
         #print noteAddr
         print channel_id,channel_name
         #print "id:",id,"comment_num:",comment_num,"visit_num:",visit_num,"like_num:",like_num
         writeImg(para_list,id,path,channel_id)
         #print_para(para_list)

# 18 21 22 28 29
# http://jfbra.com/web_page/forum_h5/post.html?post_id=91267
# http://app.jfbra.com/bra_app/post/get_share_post/16569
